package com.songlanyun.modules.opus.controller.api;

import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.enums.CommentConstant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.comment.entity.UserComment;
import com.songlanyun.modules.opus.entity.Opus;
import com.songlanyun.modules.opus.model.dto.BbsDTO;
import com.songlanyun.modules.opus.model.dto.OpusDTO;
import com.songlanyun.modules.opus.service.OpusService;
import com.songlanyun.modules.sensitive.service.SystemSensitiveWordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 作品表
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2021-01-13 16:35:02
 */
@RestController
@RequestMapping("api/opus")
public class OpusApiController extends AbstractApiController {
    @Autowired
    private OpusService opusService;
    @Autowired
    private SystemSensitiveWordService systemSensitiveWordService;

    /**
     * APP研吧中显示
     */
    @GetMapping("/all-list")
    public R allList(@RequestParam Map<String, Object> params) {
        PageUtils page = opusService.allList(params);

        return R.ok().put("page", page);
    }

    /**
     * APP研吧中根据话题来筛选
     */
    @GetMapping("/theme-list")
    public R themeList(@RequestParam Map<String, Object> params) {
        PageUtils page = opusService.themeList(params);

        return R.ok().put("page", page);
    }


    /**
     * APP查看自己发布的作品
     * 我的发布
     */
    @GetMapping("/my-release")
    public R meList(@RequestParam Map<String, Object> params) {
        Long userId = setUserId(params);
        params.put("publisherId", userId);
        //如果是自己，则可以查看所有作品，反之只能查看审核通过的
        if (!userId.equals(getUserId(false))) {
            params.put("isMe", 0);
        }
        PageUtils page = opusService.meList(params);

        return R.ok().put("page", page);
    }

    /**
     * 小程序查看作品详情
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        Opus opus = opusService.appInfo(getUserId(false), id);
        return R.ok().put("data", opus);
    }


    /**
     * 发布作品
     */
    @RepeatSubmit("3")
    @PostMapping("/add")
    //@RequiresPermissions("opus:opus:save")
    public R add(@RequestBody OpusDTO dto) {
        ValidatorUtils.validateEntity(dto);

        if(StringUtils.isNotEmpty(dto.getTitle())) {
            String title = systemSensitiveWordService.filter(dto.getTitle());
            dto.setTitle(title);
        }

        if(StringUtils.isNotEmpty(dto.getContent()))
        {
            String content = systemSensitiveWordService.filter(dto.getContent());
            dto.setContent(content);
        }

        Opus opus = opusService.add(getUserId(true), dto);
        return R.ok().put("data", opus);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    //@RequiresPermissions("opus:opus:delete")
    public R delete(@PathVariable("id") Long id) {
        opusService.deleteById(id);
        return R.ok();
    }

    //TODO 优化 - 使用redis
//    /**
//     * 浏览作品
//     */
//    @RepeatSubmit("2")
//    @PostMapping("/browse/{id}")
//    public R browse(@PathVariable("id") Long id) {
//        opusService.browse(id, getUserId(false));
//        return R.ok();
//    }
//
//    /**
//     * 点赞作品
//     */
//    @RepeatSubmit("2")
//    @PostMapping("/like/{id}")
//    public R like(@PathVariable("id") Long id) {
//        opusService.like(id, getUserId(true));
//        return R.ok();
//    }
//

    /**
     * 取消点赞作品
     */
    @RepeatSubmit("2")
    @PostMapping("/cancel-like/{id}")
    public R cancelLike(@PathVariable("id") Long id) {
        Map<String, Object> map = opusService.cancelLike(id, getUserId(true));
        return R.ok().put("map", map);
    }
//
//    /**
//     * 分享作品
//     */
//    @RepeatSubmit("2")
//    @PostMapping("/like/{id}")
//    public R reprint(@PathVariable("id") Long id) {
//        opusService.share(id, getUserId(true));
//        return R.ok();
//    }
//

    /**
     * 评论作品
     */
    @RepeatSubmit("2")
    @PostMapping("/comment")
    public R save(@RequestBody UserComment userComment) {
        ValidatorUtils.validateEntity(userComment);
        userComment.setCommentUserId(getUserId(true));
        userComment.setModelType(CommentConstant.CommentModelType.OPUS);
        opusService.comment(userComment);

        return R.ok();
    }


    /**
     * 缓存bbs
     */
    @PostMapping("/cache-bbs")
    public R cacheBbs(@RequestBody BbsDTO dto) {
        ValidatorUtils.validateEntity(dto);
        dto.setUserId(getUserId(true));
        Map<String, Object> map = opusService.cacheBbs(dto);
        if (map != null) {
            return R.ok().put("map", map);
        }
        return R.ok();
    }

}
